package org.starsource.forum.server.dao.test;

import static org.junit.Assert.*;
import java.sql.Timestamp;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import org.starsource.forum.client.exception.DataNotFoundException;
import org.starsource.forum.client.exception.DatabaseException;
import org.starsource.forum.server.domain.LoginFail;
import org.starsource.forum.server.dao.LoginFailDao;

public abstract class AbsTestLoginFailDao {

	@Autowired
	private LoginFailDao loginFailDao;
	
	@Test
	public void testSave() throws DatabaseException {
		LoginFail loginFail1 = new LoginFail("ILLEGAL_ACC_FOR_TEST","192.168.8.1",new Timestamp(new java.util.Date().getTime()/1000*1000));
		loginFailDao.save(loginFail1);
		
		LoginFail loginFail2 = loginFailDao.getLoginFail(loginFail1.getId());
		assertEquals(loginFail1,loginFail2);

		LoginFail loginFail3 = new LoginFail(null,null,null);
		loginFailDao.save(loginFail3);
		
		LoginFail loginFail4 = loginFailDao.getLoginFail(loginFail3.getId());
		assertEquals(loginFail3,loginFail4);
}

	@Test
	public void testUpdate() throws DatabaseException {
		LoginFail loginFail1 = new LoginFail("ILLEGAL_ACC_FOR_TEST","192.168.8.1",new Timestamp(new java.util.Date().getTime()/1000*1000));
		loginFailDao.save(loginFail1);
		
		int loginFailId = loginFail1.getId();
		
		try {
			Thread.sleep(100);			
		} catch (InterruptedException e) {
			fail("Test fail because thread sleep");
		}
		
		loginFail1.setAccount("ILLEGAL_ACC_FOR_TEST_NEW");
		loginFail1.setIp("10.17.42.1");
		loginFail1.setTimestamp(new Timestamp(new java.util.Date().getTime()/1000*1000));
		loginFailDao.update(loginFail1);
		
		LoginFail loginFail2 = loginFailDao.getLoginFail(loginFailId);
		assertEquals(loginFail1,loginFail2);

		loginFail1.setAccount(null);
		loginFail1.setIp(null);
		loginFail1.setTimestamp(null);
		loginFailDao.update(loginFail1);
		
		LoginFail loginFail3 = loginFailDao.getLoginFail(loginFailId);
		assertEquals(loginFail1,loginFail3);
	}

	@Test
	public void testDelete() throws DatabaseException {
		LoginFail loginFail1 = new LoginFail("ILLEGAL_ACC_FOR_TEST","192.168.8.1",new Timestamp(new java.util.Date().getTime()/1000*1000));
		loginFailDao.save(loginFail1);
		int loginFailId = loginFail1.getId();
		loginFailDao.delete(loginFail1);
		
		try {
			loginFailDao.getLoginFail(loginFailId);
			assertTrue(false);
		} catch (DataNotFoundException e) {
		}
	}

	@Test
	public void testGetLoginFail() throws DatabaseException {
		LoginFail loginFail1 = new LoginFail("ILLEGAL_ACC_FOR_TEST","192.168.8.1",new Timestamp(new java.util.Date().getTime()/1000*1000));
		loginFailDao.save(loginFail1);
		
		LoginFail loginFail2 = loginFailDao.getLoginFail(loginFail1.getId());
		assertEquals(loginFail1,loginFail2);
	}
	
}
